/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.saar.penguin.give.woz;

import de.saar.penguin.give2.mapviewer.ControllerArrowsPlugin;
import de.saar.penguin.give2.mapviewer.JMapViewer;
import de.saar.penguin.give2.mapviewer.UpdateNotificationPlugin;
import give.comm.ClientMetadata;
import give.formula.Atom;
import give.formula.Formula;
import give.formula.Orientation;
import give.formula.Position;
import give.nlgserver.NlgServerException;
import give.nlgserver.NlgServerInstance;
import give.nlgserver.NlgSystem;
import give.nlgserver.discretize.Discretizer;
import give.questionnaire.QuestionnaireData;
import give.world.World;
import java.util.List;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.UIManager;

/**
 *
 * @author koller
 */
public class WozNlgSystem extends NlgSystem {
    static {
        System.setProperty("java.awt.headless", "false");
    }
    private JMapViewer mapViewer;
    private JFrame mapViewerFrame;
    private JFrame guiFrame;
    private UpdateNotificationPlugin notificationPlugin;

    public WozNlgSystem(NlgServerInstance inst, World world, Discretizer discretizer) {
        super(inst, world, discretizer);

        // set native Swing L&F
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }
    }

    @Override
	public void connectionEstablished(QuestionnaireData arg0, ClientMetadata clientMetadata) throws NlgServerException {
        mapViewerFrame = new JFrame("Map Viewer");

        mapViewer = new JMapViewer(getWorld(), 1, 1);
        mapViewer.setShowAxisLabels(false);
        mapViewer.setAlignWithPlayer(false);

        ControllerArrowsPlugin controllerArrows = new ControllerArrowsPlugin(getWorld(), mapViewer);
        mapViewer.addMouseOverObjectListener(controllerArrows);
        mapViewer.addMapAnnotationPlugin(controllerArrows);

        notificationPlugin = new UpdateNotificationPlugin(mapViewer);
        mapViewer.addMapAnnotationPlugin(notificationPlugin);

        mapViewerFrame.add(mapViewer);

        mapViewerFrame.pack();
        mapViewerFrame.setVisible(true);
        mapViewerFrame.setResizable(false);

        guiFrame = new WozGuiFrameBareGerman(this);
        guiFrame.setVisible(true);
    }

    @Override
    public void connectionDisconnected() {
        if (guiFrame != null) {
            guiFrame.setVisible(false);
        }

        if (mapViewerFrame != null) {
            mapViewerFrame.setVisible(false);
        }
    }

    @Override
    public void handleStatusInformation(Position playerPosition, Orientation playerOrientation, List<String> visibleObjects) throws NlgServerException {
        mapViewer.repaint();
    }

    @Override
    public void handleAction(Atom actionInstance, List<Formula> updates) throws NlgServerException {
        for (Formula f : updates) {
            if (f instanceof Atom) {
                Atom a = (Atom) f;

                if (a.getPredicate().equals("state")) {
                    notificationPlugin.addUpdateAnnotation(a.getArgumentAsString(0));
                }
            }
        }

        mapViewer.repaint();
    }

    @Override
    public void handleDidNotUnderstand() throws NlgServerException {
        notificationPlugin.addHelpAnnotation();
    }

    @Override
    public void handleExternalEvent(String sourceName, Map<String, String> data)  {
        // ignore external events
    }

    public void sendMessage(String message) {
        if (!"".equals(message)) {
            send(message);
        }
    }

    public void logFailure() {
        log("fail");
    }

    public void setAlignWithPlayer(boolean b) {
        mapViewer.setAlignWithPlayer(b);
        mapViewer.repaint();
    }
}
